package cn.fz.relaxation.job;

import cn.fz.relaxation.common.model.dto.HotSearchDetailDTO;
import cn.fz.relaxation.dao.entity.RealxHotSearchDO;
import cn.fz.relaxation.service.RelaxHotSearchService;
import cn.fz.relaxation.service.convert.HotSearchConvert;
import com.google.common.collect.Lists;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

import static cn.fz.relaxation.cache.hotSearch.HotSearchCacheManager.CACHE_MAP;

public abstract class BaseHotSearchJob {
    @Autowired
    protected RelaxHotSearchService relaxHotSearchService;

    protected String resourceCode;

    protected final List<RealxHotSearchDO> searchDOList = Lists.newArrayList();

    protected abstract ReturnT<String> hotSearch(String param) throws IOException;

    protected void saveSearchListToDBAndMap(){
        // 数据加到缓存中
        CACHE_MAP.put(this.resourceCode, HotSearchDetailDTO.builder()
                // 热搜数据
                .hotSearchDTOList(searchDOList.stream().map(HotSearchConvert::toDTOWhenQuery).collect(Collectors.toList()))
                // 更新时间
                .updateTime(Calendar.getInstance().getTime())
                .build());
        // 数据持久化
        relaxHotSearchService.saveCache2DB(searchDOList);
    }
}
